package com.htht.smartschool.action.teacher;

import java.util.List;
import java.util.Map;

import com.htht.smartschool.action.baseaction.BaseAction;
import com.htht.smartschool.common.model.ParamData;
import com.htht.smartschool.common.servlet.DataContainer;
import com.htht.smartschool.model.attendance.AttendanceStatisticsData;
import com.htht.smartschool.model.card.TCard;
import com.htht.smartschool.model.course.TeacherTime;
import com.htht.smartschool.model.exam.ExamResultAll;
import com.htht.smartschool.model.exam.ExamResultData;
import com.htht.smartschool.model.exam.TExaminationresult;
import com.htht.smartschool.model.exam.TExampage;
import com.htht.smartschool.model.message.TMessage;
import com.htht.smartschool.model.teacher.TClass;
import com.htht.smartschool.model.teacher.TTeacher;
import com.htht.smartschool.service.teacher.TeacherService;
import com.htht.smartschool.util.system.GsonUtils;
import com.htht.smartschool.util.system.ResultSetToExcel;

public class TeacherAction extends BaseAction{

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public boolean processData(DataContainer obj, ParamData pd) {
		TeacherService teacherservice = new TeacherService();
		boolean flag = false;
		TTeacher tteacher = null;
		try {
			tteacher = (TTeacher) GsonUtils.getInstance().fromJson(
					pd.getData(), TTeacher.class);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if(pd.getCmd().equalsIgnoreCase("addTeacher")){
			boolean result = teacherservice.addTeacher(tteacher);
			if(result){
				flag = true;
				obj.setData(result);
			}else{
				obj.setData("添加失败！");
			}
		}else if(pd.getCmd().equalsIgnoreCase("updateTeacher")){
			TTeacher result = teacherservice.updateTeacher(tteacher);
			if (result != null) {
				flag = true;
				obj.setData(result);
			} else {
				obj.setData("修改失败！");
			}
		}else if(pd.getCmd().equalsIgnoreCase("deleteTeacher")){
			boolean result = teacherservice.deleteTeacher(tteacher);
			if (result) {
				flag = true;
				obj.setData(result);
			} else {
				obj.setData("删除失败！");
			}
		}else if(pd.getCmd().equalsIgnoreCase("delete")){
			boolean result = teacherservice.deleteinfo(tteacher.getTeachercode(),tteacher.getCellphone());
			if (result) {
				flag = true;
				obj.setData(result);
			} else {
				obj.setData("删除失败！");
			}
		}else if(pd.getCmd().equalsIgnoreCase("getTeacher")){
			TTeacher teacher = teacherservice.getTeacher(tteacher.getCellphone());
			if (teacher != null) {
				flag = true;
				obj.setData(teacher);
			} else {
				obj.setData("查无数据！");
			}
		}else if(pd.getCmd().equalsIgnoreCase("getClassAttendanceByTeacherCode")){
			List<AttendanceStatisticsData> teacher = teacherservice.getClassAttendanceByTeacherCode(tteacher);
			if (teacher.size()>0) {
				flag = true;
				obj.setData(teacher);
			} else {
				obj.setData("查无数据！");
			}
		}else if(pd.getCmd().equalsIgnoreCase("getTeacherTimeByTeacherCode")){
			List<TeacherTime> teacher = teacherservice.getTeacherTimeByTeacherCode(tteacher.getTeachercode());
			if (teacher.size()>0) {
				flag = true;
				obj.setData(teacher);
			} else {
				obj.setData("查无数据！");
			}
		}else if(pd.getCmd().equalsIgnoreCase("getAllTeachers")){
			Map map = teacherservice.getAllTeachers(tteacher);
			if (map != null) {
				flag = true;
				obj.setData(map);
			} else {
				obj.setData("查无数据！");
			}
		}else if(pd.getCmd().equalsIgnoreCase("getAllClasses")){
			List<TClass> allclasses = teacherservice.getAllClasses(tteacher.getTeachercode());
			if (allclasses.size() > 0) {
				flag = true;
				obj.setData(allclasses);
			} else {
				obj.setData("查无数据！");
			}
		}else if(pd.getCmd().equalsIgnoreCase("getExamsByClassCode")){
			TExaminationresult texaminationresult = (TExaminationresult) GsonUtils.getInstance().fromJson(pd.getData(), TExaminationresult.class);
			List<ExamResultData> teacher = teacherservice.getExamsByClassCode(texaminationresult.getClasscode());
			if (teacher.size()>0) {
				flag = true;
				obj.setData(teacher);
			} else {
				obj.setData("查无数据！");
			}
		}else if(pd.getCmd().equalsIgnoreCase("senIntoSchoolMessge")){
			TCard card = (TCard) GsonUtils.getInstance().fromJson(pd.getData(), TCard.class);
			String result = teacherservice.senIntoSchoolMessge(card);
			if(result != null && !result.equalsIgnoreCase("")){
				flag = true;
				obj.setData(result);
			}else{
				obj.setData("发送失败");
			}
		}else if(pd.getCmd().equalsIgnoreCase("sendMessage")){
			TMessage tme = (TMessage) GsonUtils.getInstance().fromJson(pd.getData(), TMessage.class);
			String result = teacherservice.sendMessage(tme);
			if(result != null && !result.equalsIgnoreCase("")){
				flag = true;
				obj.setData(result);
			}else{
				if(teacherservice.getMsg().length() > 0){
					obj.setData(teacherservice.getMsg());
					teacherservice.setMsg("");
				}else{
					obj.setData("发送失败");
				}
			}
		}else if(pd.getCmd().equalsIgnoreCase("getMessageByMessageId")){
			TMessage tme = (TMessage) GsonUtils.getInstance().fromJson(pd.getData(), TMessage.class);
			List<TMessage> result = teacherservice.getMessageByMessageId(tme);
			if(result != null && result.size() > 0){
				flag = true;
				obj.setData(result);
			}else{
				obj.setData("查询失败");
			}
		}else if(pd.getCmd().equalsIgnoreCase("getMessage")){
			TMessage tme = (TMessage) GsonUtils.getInstance().fromJson(pd.getData(), TMessage.class);
			List<TMessage> result = teacherservice.getMessage(tme);
			if(result != null && result.size() > 0){
				flag = true;
				obj.setData(result);
			}else{
				obj.setData("查询失败");
			}
		}else if(pd.getCmd().equalsIgnoreCase("batchDeleteTeachersByCodes")){
			flag = teacherservice.batchDeleteTeachersByCodes(tteacher);
			if(flag){
				obj.setData("删除成功");
			}else{
				obj.setData("批量删除失败");
			}
		}else if(pd.getCmd().equalsIgnoreCase("unLockTeachersByCodes")){
			flag = teacherservice.unLockTeachersByCodes(tteacher);
			if(flag){
				obj.setData("解锁成功");
			}else{
				obj.setData("解锁失败");
			}
		}else if(pd.getCmd().equalsIgnoreCase("getExamsByPage")){
			TExampage tExampage = (TExampage) GsonUtils.getInstance().fromJson(pd.getData(), TExampage.class);
			Long currentpage = tExampage.getCurrentpage();
			Long pageSize = tExampage.getPagesize();
			String subjectCode = tExampage.getSubjectcode();
			Long startTime = tExampage.getStarttime();
			Long endTime = tExampage.getEndtime();
			String schoolcode = tExampage.getSchoolcode();
			List<TExampage> teacher = teacherservice.getExamsByPage(subjectCode,startTime,endTime,currentpage,pageSize,schoolcode);
			if (teacher.size()>0) {
				flag = true;
				obj.setData(teacher);
			} else {
				obj.setData("查无数据！");
			}
		}else if(pd.getCmd().equalsIgnoreCase("getExamsresultAll")){
			ExamResultAll examResultAll = (ExamResultAll) GsonUtils.getInstance().fromJson(pd.getData(), ExamResultAll.class);
			Map map = teacherservice.getExamsresultAll(examResultAll);
			List<ExamResultAll> teacher = (List<ExamResultAll>) map.get("list");
			ResultSetToExcel rse = (ResultSetToExcel) map.get("remark");
			if (teacher.size()>0) {
				flag = true;
				obj.setData(teacher);
				if(rse != null){
					obj.setRemark(rse);
				}
			} else {
				obj.setData("查无数据！");
			}
		}else if(pd.getCmd().equalsIgnoreCase("getExamsresultAll_Model")){
			ExamResultAll examResultAll = (ExamResultAll) GsonUtils.getInstance().fromJson(pd.getData(), ExamResultAll.class);
			ResultSetToExcel rse = teacherservice.getExamsresultAll_Model(examResultAll);
			if (rse != null) {
				flag = true;
				obj.setData(rse);
			} else {
				obj.setData("查无数据！");
			}
		}else if(pd.getCmd().equalsIgnoreCase("addExam")){
			ExamResultAll examResultAll = (ExamResultAll) GsonUtils.getInstance().fromJson(pd.getData(), ExamResultAll.class);
			List list = teacherservice.examJudge(examResultAll.getTimestr());
			if(list.size()==0){
				 boolean i = teacherservice.addExam(examResultAll);
				if (i) {
					flag = true;
					obj.setData(i);
				} else {
					obj.setData("添加失败！");
				}
			}else{
				obj.setData("已添加过此考试！");
			}
		}else if(pd.getCmd().equalsIgnoreCase("addExaminationresult")){
			TExaminationresult tExaminationresult = (TExaminationresult) GsonUtils.getInstance().fromJson(pd.getData(), TExaminationresult.class);
			List list = teacherservice.examJudge(tExaminationresult);
			if(list.size()==0){
				int i = teacherservice.addExaminationresult(tExaminationresult);
				if (i > 0) {
					flag = true;
					obj.setData(i);
				} else {
					obj.setData("添加失败！");
				}
			}else{
				obj.setData("已添加过此学生成绩！");
			}
			
		}else if(pd.getCmd().equalsIgnoreCase("updateExam")){
			ExamResultAll examResultAll = (ExamResultAll) GsonUtils.getInstance().fromJson(pd.getData(), ExamResultAll.class);
			int i = teacherservice.updateExam(examResultAll);
			if (i > 0) {
				flag = true;
				obj.setData(i);
			} else {
				obj.setData("修改考试失败！");
			}
		}else if(pd.getCmd().equalsIgnoreCase("updateExamresults")){
			TExaminationresult tExaminationresult = (TExaminationresult) GsonUtils.getInstance().fromJson(pd.getData(), TExaminationresult.class);
			int i = teacherservice.updateExamresult(tExaminationresult);
			if (i > 0) {
				flag = true;
				obj.setData(i);
			} else {
				obj.setData("修改考试成绩失败！");
			}
		}else if(pd.getCmd().equalsIgnoreCase("deleteExam")){
			ExamResultAll examResultAll = (ExamResultAll) GsonUtils.getInstance().fromJson(pd.getData(), ExamResultAll.class);
			int i = teacherservice.deleteExam(examResultAll);
			if(i>0){
				int j = teacherservice.deleteExamresult(examResultAll);
				if(j>0){
					flag = true;
					obj.setData(j);
				}else{
					obj.setData("删除考试成绩失败！");
				}
			}else{
				obj.setData("删除考试失败！");
			}
		}else if(pd.getCmd().equalsIgnoreCase("deleteExamresult")){
			ExamResultAll examResultAll = (ExamResultAll) GsonUtils.getInstance().fromJson(pd.getData(), ExamResultAll.class);
			int i = teacherservice.deleteExamresult(examResultAll);
			if(i>0){
				flag = true;
				obj.setData(i);
			}else{
				obj.setData("删除考试成绩失败！");
			}
		}else if(pd.getCmd().equalsIgnoreCase("exportTable")){
			ResultSetToExcel res = teacherservice.exportTable(tteacher.getName());
			if(res != null){
				obj.setData(res);
			}else{
				obj.setData("删除考试成绩失败！");
			}
		}else if(pd.getCmd().equalsIgnoreCase("queryAllTeacher")){
			String schoolcode = tteacher.getSchoolcode();
			List<TTeacher> list = teacherservice.queryAllTeacher(schoolcode);
			if(list.size() > 0){
				flag=true;
				obj.setData(list);
			}else{
				obj.setData("查询失败");
			}
		}else if(pd.getCmd().equalsIgnoreCase("getTeacherByClasscode")){
			List<TTeacher> list = teacherservice.getTeacherByClasscode(tteacher);
			if(list.size() > 0){
				flag = true;
				obj.setData(list);
			}else{
				obj.setData("查询失败");
			}
		}else if(pd.getCmd().equalsIgnoreCase("recovery")){
			List list = teacherservice.recovery();
			if(list.size() > 0){
				flag = true;
				obj.setData(list);
			}else{
				obj.setData("查询失败");
			}
		}
		return flag;
	}
}
